package com.tree.array;

import java.util.*;

/**
 * package com.tree.array
 * description:
 *
 * 逆转单链表， 双向链表
 *
 * @author tree
 * @date 2019-10-14 10:48
 */
public class ReverseList {

    class SingleNode{
        public int value;
        public SingleNode next;
    }

    class DoubleNode{
        public int value;
        public DoubleNode next;
        public DoubleNode last;
    }

    /**
     * 反转单链表：
     *  head赋值为next（next= head.next）
     *  head.next赋值为以前的head
     * @param head
     */
    public void reverseSingle(SingleNode head){
        SingleNode pre = null;
        SingleNode next = null;

        while (head!= null){
            pre = head;
            next = head.next;

            head=next;
            head.next = pre;
        }
    }

    public void reverseDouble(DoubleNode head){
        DoubleNode pre = null;
        DoubleNode next = null;

        while (head!= null){
            next = head.next;
            head.next = pre;
            head.last = next;

            pre = head;
            head=next;

        }

    }

    public static int[][] get2DArray(Scanner sc, int m, int n){
//        Scanner sc=new Scanner(System.in);
//        int m=sc.nextInt();
//        int n=sc.nextInt();
        int map[][]=new int[m][n];
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                map[i][j]=sc.nextInt();
            }
        }
        return map;
    }

    public static int[][] getDefault2DArray(int m, int n){
        Scanner sc=new Scanner(System.in);
//        int m=sc.nextInt();
//        int n=sc.nextInt();
        int map[][]=new int[m][n];
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                map[i][j]=sc.nextInt();
            }
        }
        return map;
    }


    public static int getMin(int[][] map, int n, int m) {
        int[][] dp = new int[n][m];
        for(int i=0;i<n;i++){        //初始化第一列的值
            for(int j=0;j<=i;j++){
                dp[i][0]+=map[j][0];
            }
        }
        for(int i=0;i<m;i++){       //初始化第一行的值
            for(int j=0;j<=i;j++){
                dp[0][i]+=map[0][j];
            }
        }
        for(int i=1;i<n;i++){
            for(int j=1;j<m;j++){

                dp[i][j] = min(dp[i][j-1]+map[i][j],dp[i-1][j]+map[i][j]);
                System.out.println("i="+i+",j="+j+",dp[i][j]="+dp[i][j]);
            }
        }
        return dp[n-1][m-1];
    }
    public static int min(int a,int b){
        if(a>b){
            return b;
        }else{
            return a;
        }
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        int n=sc.nextInt();
        int map[][]=get2DArray(sc,m,n);
        System.out.println(getMin(map,m,n));



    }
}
